package pkg.andru.widget;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

public class TabWidgetFragment extends Fragment implements OnTouchListener {

    static final String TAG = "TabWidgetFragment";

    int mCurrentTab;
    int mTabSize;
    int mContainerId;
    boolean mIsClickEnable = true;
    View mView;
    Rect mViewRect = new Rect();
    int mTouchTabIndex;
    int mLayout;

    OnTabChangedCallback mCallback;
    Drawable mDrawable;
    boolean isViewAreaNeedInited;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(mLayout, container, false);
        initUI(view);
        return view;
    }

    private void initUI(View view) {
        mView = view;
        // View frag = mView.findViewById(R.id.fragment_tab_widget);
        View frag = null;
        mDrawable = frag.getBackground();

        frag.setFocusable(true);
        frag.setOnTouchListener(this);
        frag.setClickable(true);
        updateTab(mCurrentTab);
        isViewAreaNeedInited = true;
    }

    private void initViewArea(View view) {
        if (isViewAreaNeedInited) {
            int[] location = new int[2];
            view.getLocationOnScreen(location);

            mViewRect.left = view.getLeft();
            mViewRect.top = view.getTop();
            mViewRect.right = mViewRect.left + view.getWidth();
            mViewRect.bottom = mViewRect.top + view.getHeight();
            isViewAreaNeedInited = false;
        }
    }

    @Override
    public void onDestroyView() {
        mView = null;
        isViewAreaNeedInited = true;
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        mCallback = null;
        super.onDetach();
    }

    public void setCurrentTab(int tab) {
        mCurrentTab = tab;
    }

    public void setTabSize(int size) {
        mTabSize = size;
    }

    public void setTabClickable(boolean click) {
        mIsClickEnable = click;
    }

    public void setOnTabChangedCallback(OnTabChangedCallback callback) {
        mCallback = callback;
    }

    public void setWidgetLayout(int layout) {
        mLayout = layout;
    }

    void updateTab(int newTab) {
        mCurrentTab = newTab;
        if (mDrawable != null) {
            mDrawable.setLevel(mCurrentTab);
        }
    }

    public interface OnTabChangedCallback {
        public void onTabChanged(int before, int after);
    }

    boolean isInViewArea(int x, int y) {
        return mViewRect.contains(x, y);
    }

    int getTabIndex(int x, int y) {
        final int tab_width = (mViewRect.right - mViewRect.left) / mTabSize;
        return (x - mViewRect.left) / tab_width;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mIsClickEnable && mTabSize > 0 && mView != null) {
            initViewArea(v);

            final int action = event.getAction();
            int px = (int) event.getX();
            int py = (int) event.getY();

            // only process view
            if (isInViewArea(px, py)) {
                if (action == MotionEvent.ACTION_DOWN) {
                    mTouchTabIndex = getTabIndex(px, py);
                } else if (action == MotionEvent.ACTION_UP) {
                    int up_index = getTabIndex(px, py);
                    // Log.i(TAG, String.format("touch[down:%d,up%d]",
                    // mTouchTabIndex, up_index));
                    if (mTouchTabIndex == up_index && mTouchTabIndex != mCurrentTab) {
                        if (mCallback != null) {
                            mCallback.onTabChanged(mCurrentTab, mTouchTabIndex);
                        }
                        // update drawable
                        updateTab(mTouchTabIndex);

                        // play the sound
                        if (v.isSoundEffectsEnabled()) {
                            v.playSoundEffect(SoundEffectConstants.CLICK);
                        }
                    }
                }
                return true;
            }
        }
        return false;
    }
}
